<?php

namespace app\controller\api\user;


use app\common\repositories\user\UserIntegralBillRepository;
use app\common\repositories\user\UserIntegralConfigRepository;
use app\common\repositories\user\UserRepository;
use crmeb\basic\BaseController;
use app\common\repositories\user\UserSignRepository;
use think\App;
use think\facade\Cache;
use think\facade\Db;

class IntegralTask extends BaseController
{
    /**
     * @var repository
     */
    protected $repository;

    /**
     * UserSign constructor.
     * @param App $app
     * @param UserSignRepository $repository
     */
    public function __construct(App $app, UserSignRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * @return mixed
     * @author: fupeng
     * @day: 2024/3/22
     * 任务列表
     */
    public function lst()
    {
        $data = app()->make(UserIntegralConfigRepository::class)->apiList($this->request->userInfo());
        return app('json')->success($data);
    }

    public function share()
    {
        $config = userIntegralConfig('share');
        if($config['status'] == 1 && $config['value'] > 0) {
            $userInfo = $this->request->userInfo();
            $num = app()->make(UserIntegralBillRepository::class)->getCount(['category' => 'integral','date'=>'today','type' => 'share','uid' => $userInfo->uid]);
            if($num < $config['num']){
                app()->make(UserIntegralBillRepository::class)->incBill($userInfo->uid, 'integral', 'share', [
                    'link_id' => 0,
                    'status' => 1,
                    'title' => '分享转发送积分',
                    'number' => $config['value'],
                    'mark' => '分享转发获赠' . floatval($config['value']) . '积分' ,
                    'balance' => $userInfo->integral
                ]);
                $make = app()->make(UserRepository::class);
                $make->update($userInfo->uid, ['integral' => Db::raw('integral+' . $config['value'])]);
                return app('json')->success('分享成功');
            }
            return app('json')->fail('当日次数已用完');
        }
        return app('json')->fail('数据错误');
    }

    public function getKeyResult(){
        $key = $this->request->param('key');
        $is_verify = $this->request->param('is_verify',0);
        $config = userIntegralConfig($key);
        if(!$is_verify){
            if($config['status'] == 1 && $config['value'] > 0 && in_array($key,['share','watch_video','seckill_product','browse_product','browse_ad'])) {
                $userInfo = $this->request->userInfo();
                $num = app()->make(UserIntegralBillRepository::class)->getCount(['category' => 'integral','date'=>'today','type' => $key,'uid' => $userInfo->uid]);
                if($num >= $config['num']){
                    return app('json')->fail('当日次数已用完');
                }
                $config['surplus_num'] = $config['num'] - $num;
                $config['sign'] = 'task_integral_' . $userInfo->uid . '_' . $key. rand(00000,99999);
                Cache::set($config['sign'], ['time'=>$config['ask'],'start_time'=>time()], 600);
                return app('json')->success($config);
            }
            return app('json')->fail('数据错误');
        }else{
            return app('json')->success($config);
        }


    }


    /**
     * 通知结果
     * @return mixed
     * @author: fupeng
     * @day: 2024/3/26
     */
    public function noticeResult(){
        $key = $this->request->param('key');
        $sign = $this->request->param('sign');
        $config = userIntegralConfig($key);
        $uid = $this->request->uid();
        if(in_array($key,['watch_video','seckill_product','browse_product'])){
            $res = Cache::get($sign);
            if(!$res){
                return app('json')->fail('已失效');
            }
            if((time() - $res['start_time']) < $res['time']){
                return app('json')->fail('时间不足'.$res['time'].'秒');
            }
        }
        if($config['status'] == 1 && $config['value'] > 0 && in_array($key,['watch_video','seckill_product','browse_product','browse_ad'])) {
            $num = app()->make(UserIntegralBillRepository::class)->getCount(['category' => 'integral','date'=>'today','type' => $key,'uid' => $uid]);
            if($num >= $config['num']){
                return app('json')->fail('当日次数已用完');
            }
            app()->make(UserIntegralBillRepository::class)->userKeyIntegral($uid,$config,$key);
            Cache::delete($sign);
            return app('json')->success('ok');
        }
        return app('json')->fail('数据错误');
    }
}
